package com.hexacta.hrs.web.struts.actions.feedbacks;

import java.util.Date;
import java.util.List;

import org.apache.poi.ss.usermodel.Row;
import org.vectrics.user.Person;

import com.hexacta.hrs.services.person.PersonService;

public class TrainingRowData {

    private Date date;

    private Person person;

    private String trainingName;

    private PersonService personService;

    public TrainingRowData(final PersonService personService, final Row data) {

        this.personService = personService;

        setPerson(data);
        setDate(data);
        setTrainingName(data);

    }

    /**
     * @param data
     */
    private void setTrainingName(final Row data) {
        trainingName = data.getCell(3).getStringCellValue();
    }

    /**
     * @param data
     * @return
     */
    private void setDate(final Row row) {
        date = row.getCell(2).getDateCellValue();
    }

    private void setPerson(final Row row) {
        // en el excel solo hay nombre y apellido, no segundo nombre. uso la
        // primer palabra del
        // campo de nombre para matchear por nombre.
        String nombre = row.getCell(0).getStringCellValue().split(" ")[0];
        String apellido = row.getCell(1).getStringCellValue();

        Person searchPerson = new Person();
        searchPerson.setFirstName(nombre + "%");
        searchPerson.setLastName(apellido + "%");
        List<Person> persons = (List<Person>) personService.matching(searchPerson);

        if (persons.size() != 1) {
            person = null;
        } else {
            person = persons.get(0);
        }
    }

    /**
     * @return the date
     */
    public Date getDate() {
        return date;
    }

    /**
     * @return the person
     */
    public Person getPerson() {
        return person;
    }

    /**
     * @return the trainingName
     */
    public String getTrainingName() {
        return trainingName;
    }

}
